// pages/couponList/couponList.js
import { storeApi } from "../../../api/index";
// 获取app
var app = getApp();
Page({
  data: {
    filesHost: app.globalData.filesHost,
    pageParam: {
      page: 1,
      page_size: 10
    },
    couponId: 1, //couponId 1为待使用 2为已失效
    couponType: "unuse",
    canuse: {
      list: [],
      isEmpty: 1,
      hasMore: 1,
    },
    invalid: {
      list: [],
      isEmpty: 1,
      hasMore: 1,
    },
    shareInfo: {},
  },
  changeClass(e) {
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    this.setData({
      couponId: id,
      couponType: type,
    });
    if (id == 1 && this.data.canuse.hasMore == 0) {
      return;
    }
    if (id == 2 && this.data.invalid.hasMore == 0) {
      return;
    }
    this.getCouponList(id);
  },
  getCouponList(id = 1) {
    storeApi.queryUserCouponList({
      ...this.data.pageParam,
      use_status: +this.data.couponId,
    }).then((res) => {
      if (id == 1) {
        this.setData({
          canuse: {
            list: res.list,
            isEmpty: res.list.length > 0 ? 1 : 0,
            hasMore: 0,
          },
        });
      } else {
        this.setData({
          invalid: {
            list: res.list,
            isEmpty: res.list.length > 0 ? 1 : 0,
            hasMore: 0,
          },
        });
      }
    });
  },
  async checkOrder(){
    try {
      let res = res = await storeApi.queryRanstoreReservationOrderList({
        page: 1,
        page_size: 20,
        status: 4 // 获取用户待支付订单
      })
      if(res.list.length > 0){
         wx.navigateTo({
          url: `/pages/order/order?checkedTab=4`,
        });
      } else {
        wx.navigateTo({
          url: `/pages/book-step/book-step`,
        });
      }
    } catch (e) { 
      console.log(e)
    }
  },
  goUse(e) {
    let {
      id,
      coupon_id,
    } = e.currentTarget.dataset;
    // wx.navigateTo({
    //   url: `/pages/book-step/book-step?coupon_id=${coupon_id}`,
    // });
    this.checkOrder()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCouponList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '频繁染白发，就找染博士',
      imageUrl:
        'https://ranboss.oss-cn-beijing.aliyuncs.com/ranstore/share.png',
    };
  },
});
